// 基础数据-仓库管理
import React, { Component } from 'react'
import {ListBox} from '../../../components'
import UpdateWareHouse from './Update'
import { fetchData } from '../../../components/AjaxConsumer'
import { Modal } from 'antd';

export default class WareHouse extends Component{
    constructor(props) {
        super(props);
        //行操作列表
        this.rowOperation = [
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'addWareHouse',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit'
            },{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        // foreverHidden永久隐藏
        this.tableColumn = {
            storageKey: 'ListWareHouseColumnConfig',
            data: [
                {
                    showName: 'id',
                    columnName: 'warehouseId',
                    columnHidden:true,
                    foreverHidden: true
                },
                {
                    showName: '仓库编码',
                    columnName: 'warehouseCode'
                },{
                    showName: '仓库名称',
                    columnName: 'warehouseName'
                },{
                    showName: '仓库类型',
                    columnName: 'warehouseType',
                    showOption:[
                        {
                            optionKey:'1',
                            optionShow:'原材料'
                        },{
                            optionKey:'2',
                            optionShow:'成品'
                        }
                    ]
                },
                // {
                //     showName: '工厂编码',
                //     columnName: 'factoryCode'
                // },{
                //     showName: '工厂名称',
                //     columnName: 'factoryName'
                // },
                
                {
                    showName: '激活标记',
                    columnName: 'activeFlag',
                    showOption:[
                        {
                            optionKey:'1',
                            optionShow:'启用'
                        },{
                            optionKey:'2',
                            optionShow:'禁用'
                        }
                    ]
                },{
                    showName: '创建人',
                    columnName: 'createBy',
                    columnHidden:true
                },{
                    showName: '创建时间',
                    columnName: 'gmtCreate',
                    columnHidden:true
                },{
                    showName: '更新人',
                    columnName: 'lastModifiedBy',
                    columnHidden:true
                },{
                    showName: '更新时间',
                    columnName: 'gmtModified',
                    columnHidden:true
                }
            ]
        };

        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        this.searchComponentData = [
            {
                showName: '仓库编码',
                name:'warehouseCode',
                type: 'input',
                url: null
            },{
                showName: '仓库名称',
                name:'warehouseName',
                type: 'input',
                url: null
            },
            // {
            //     showName: '工厂编码',
            //     name:'factoryCode',
            //     type: 'input',
            //     url: null
            // },{
            //     showName: '工厂名称',
            //     name:'factoryName',
            //     type: 'input',
            //     url: null
            // },
            {
                name:'warehouseType',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'1',
                        optionShow:'原材料'
                    },{
                        optionKey:'2',
                        optionShow:'成品'
                    }
                ]
            },
            {
                name:'activeFlag',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'1',
                        optionShow:'启用'
                    },{
                        optionKey:'2',
                        optionShow:'禁用'
                    }
                ]
            },{
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax', //最大值， 选择小于时使用；选择范围时大值使用
                minName:'gmtCreateMin'  //最小值， 选择大于时使用；选择范围时小值使用
            },{
                name:'gmtModified',
                type: 'date',
                maxName:'gmtModifiedMax', 
                minName:'gmtModifiedMin'
            }
        ];

    }

// 行操作
    rowHandles = (strHandles, rowObj) => {
        if(strHandles === 'edit'){
            this.editHandle(rowObj);
        }else if(strHandles === 'delete'){
            this.deleteHandle(rowObj);
        }else if(strHandles === 'addWareHouse'){
            this.addHandle();
        }
    }

    addHandle = () => {
        this.refs.updateWareHouse.getFormData[1].disable = false;
        this.refs.updateWareHouse.addHandle();
    }

    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateWareHouse.getFormData[1].disable = true;
            this.refs.updateWareHouse.editHandle(rowObj[0].data);
        }
    }

    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateWareHouse.deleteHandle(rowObj[0].data);
        }
    }

    //刷新列表
    refreshHandle = () => {
        this.refs.warehouseList.refreshHandle();
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <div>
                <ListBox
                    ref = 'warehouseList'
                    isHideLeft={isHideLeft}
                    title="仓库管理"
                    tableColumnData = {this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url = '/base/data/warehouse/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    isRowSingle = "true"
                />

                <UpdateWareHouse
                    ref="updateWareHouse"
                    refreshHandle = {this.refreshHandle}
                />

            </div>
        )
    }

}
